package thesis.prototype;

import java.security.acl.Owner;

import thesis.prototype.objects.ConnectPHP;
import thesis.prototype.objects.Meeting;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MeetingDetailsActivity extends Activity implements OnClickListener{

	// UI elements
	private Button b_meeting_accept;
	private Button b_meeting_reject;
	private Button b_meeting_back;
	private EditText e_meeting_title;
	private EditText e_meeting_date;
	private EditText e_meeting_time;
	private EditText e_meeting_location;
	
	// DB connectivity
	ConnectPHP DBConnect;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);		
		setContentView(R.layout.meeting);
		
		b_meeting_accept = (Button) findViewById(R.id.Accept);
		b_meeting_reject = (Button) findViewById(R.id.Reject);
		b_meeting_back = (Button) findViewById(R.id.backToMeetingList);
		
		e_meeting_title = (EditText) findViewById(R.id.meeting_title);
		e_meeting_date = (EditText) findViewById(R.id.meeting_date);
		e_meeting_time = (EditText) findViewById(R.id.meeting_time);
		e_meeting_location = (EditText) findViewById(R.id.meeting_location);
		
		DBConnect = ConnectPHP.getInstance();
		
		setUpEditTexts();
		addButtonListeners();
	}
	
	private void setUpEditTexts()
	{
		Meeting theMeeting = HelloActivity.viewing_meeting;
		
		e_meeting_title.setText(theMeeting.getAgenda());
		e_meeting_title.setFocusable(false);
		e_meeting_title.setClickable(false);
		
		//e_meeting_date.setText(theMeeting.getTimedate());
		String timedate = theMeeting.getTimedate();
		String date = timedate.split(" ")[0];
		String time = timedate.split(" ")[1];
		e_meeting_date.setText(date);
		e_meeting_date.setFocusable(false);
		e_meeting_date.setClickable(false);
				
		e_meeting_time.setText(time);
		e_meeting_time.setFocusable(false);
		e_meeting_time.setClickable(false);
		
		e_meeting_location.setText(theMeeting.getLocation());
		e_meeting_location.setFocusable(false);
		e_meeting_location.setFocusable(false);
	}
	
	private void addButtonListeners()
	{
		if (b_meeting_accept != null) b_meeting_accept.setOnClickListener(this);
		if (b_meeting_reject != null) b_meeting_reject.setOnClickListener(this);
		if (b_meeting_back != null) b_meeting_back.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		if(v==b_meeting_accept)
		{
			DBConnect.updateStatus(HelloActivity.ownNumber, HelloActivity.viewing_meeting.getMeeting_id(), "accepted");
			setResult(RESULT_OK);
			finish();
		}
		else if(v==b_meeting_reject)
		{
			DBConnect.updateStatus(HelloActivity.ownNumber, HelloActivity.viewing_meeting.getMeeting_id(), "rejected");
			setResult(RESULT_OK);
			finish();
		}
		else if(v==b_meeting_back)
		{
			setResult(RESULT_CANCELED);
			finish();
		}
	}
}
